package com.tiancheng.trade.merchant.controller.api;


import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.merchant.exception.BusinessException;
import com.tiancheng.trade.merchant.model.Product;
import com.tiancheng.trade.merchant.service.IProductService;
import com.tiancheng.trade.merchant.vo.merchant.GetProductVO;
import com.tiancheng.trade.merchant.vo.product.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 根据交易中台商品编号查询商品和支付渠道信息
     *
     * @param list {@link List}<{@link String}> 交易中台商品编号列表
     * @return {@link Result}<{@link List}<{@link Product}>> 商品和支付渠道信息列表
     */
    @GetMapping("/findPaymentChannelByProductCodes")
    public Result<List<Product>> findPaymentChannelByProductCodes(@RequestParam("list") List<String> list)
            throws BusinessException {

        return Result.success(productService.findPaymentChannelByProductCodes(list));
    }

    /**
     * 根据业务侧商品编码查询商品和支付渠道信息
     *
     * @param list {@link List}<{@link String}> 业务侧商品编号列表
     * @return {@link Result}<{@link List}<{@link Product}>> 商品和支付渠道信息列表
     */
    @GetMapping("/findPayChannelByCodeBusiness")
    public Result<List<Product>> findPayChannelByCodeBusiness(@RequestParam("list") List<String> list)
            throws BusinessException {

        return Result.success(productService.findPayChannelByCodeBusiness(list));
    }


    @GetMapping("/{id}")
    public Result<FindProductDetailResponseVO> findDetail(@PathVariable("id") Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        return Result.success(productService.findDetailById(id));
    }

    /**
     * 根据交易中台商户编号和支付渠道查询商品数量
     *
     * @param productVO {@link GetProductVO}
     * @return 商品数量
     */
    @GetMapping("/getProductQty")
    public Result<Long> getProductQty(GetProductVO productVO) throws BusinessException {
        Assert.isTrue(productVO.getMerchantCode() != null || productVO.getPayChannel() != null, "参数错误~");
        return Result.success(productService.getProductQty(productVO));
    }


    /**
     * 根据交易中台商户编号和支付渠道查询商品数量
     *
     * @param productId 产品id
     * @return 商品数量
     */
    @GetMapping("/getProductInfo")
    public Result<Product> getProductQty(@RequestParam("productId") String productId) throws BusinessException {
        return Result.success(productService.getProductInfo(productId));
    }


    /**
     * 新增
     *
     * @param productVO {@link CreateProductVO}
     */
    @PostMapping("/openAdd")
    public Result<List<CreateProductResVO>> create(@RequestBody @Valid OpenCreateProductVO productVO) throws BusinessException {
        List<CreateProductResVO> productResVOS = productService.openCreate(productVO);
        return Result.success(productResVOS);
    }

    /**
     * 新增
     *
     * @param productVO {@link CreateProductVO}
     */
    @PostMapping("/openUpdate")
    public Result<String> openUpdate(@RequestBody @Valid OpenUpdateProductVO productVO) throws BusinessException {
        productService.openUpdate(productVO);
        return Result.success("修改成功");
    }

    /**
     * 删除
     *
     * @param openDeleteProductVO {@link CreateProductVO}
     */
    @PostMapping("/openDelete")
    public Result<String> openDelete(@RequestBody @Valid OpenDeleteProductVO openDeleteProductVO) throws BusinessException {
        productService.openDelete(openDeleteProductVO);
        return Result.success("删除成功");
    }


    /**
     * 删除
     *
     * @param openDeleteProductVO {@link CreateProductVO}
     */
    @PostMapping("/openDeleteByMerchant")
    public Result<String> openDeleteByMerchant(@RequestBody @Valid OpenDeleteProductByMerchantVO openDeleteProductVO) throws BusinessException {
        productService.openDeleteByMerchant(openDeleteProductVO);
        return Result.success("删除成功");
    }

}
